﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Timers;
using System.Threading;

namespace EloamDllDemo
{
    using HELOAMIMAGE = IntPtr;
    using HELOAMIMAGELIST = IntPtr;
    using HELOAMFTP = IntPtr;
    using HELOAMHTTP = IntPtr;
    using HELOAMDEVICE = IntPtr;
    using HELOAMVIDEO = IntPtr;
    using HELOAMVIEW = IntPtr;
    using HELOAMMEMORY = IntPtr;
    using HELOAMBASE64 = IntPtr;
    using HELOAMTHUMBNAIL = IntPtr;
    using HELOAMRECT = IntPtr;
    using HELOAMFONT = IntPtr;
    using HELOAMVIDEOCAP = IntPtr;
    using LPVOID = IntPtr;
    using LONG = Int32;
    using BOOL = Int32;
    using HWND = IntPtr;
    using COLORREF = UInt32;

    // callback
    public delegate void ELOAM_DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData);
    public delegate void ELOAM_ARRIVALCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData);
    public delegate void ELOAM_TOUCHCALLBACK(HELOAMVIDEO video, LPVOID userData);
    public delegate void ELOAM_MOVEDETECCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData);
    public delegate void ELOAM_ATTACHCALLBAK(HELOAMVIDEO video, LONG videoId, HELOAMVIEW view, LONG viewId, LPVOID userData);

    public delegate void ELOAM_IDCARDCHANGECALLBACK(LONG dbt, LPVOID userData);
    public partial class Form1 : Form
    {

        HELOAMVIEW m_hView;
        HELOAMTHUMBNAIL m_hThumb;

        //设别列表（专指视频）
        List<HELOAMDEVICE> m_vDevice;
        HELOAMVIDEO m_hVideo;
        HELOAMVIDEOCAP m_cap;
        //合成时的临时图片
        HELOAMIMAGE m_hImageTemp;

        //定义系统计时器
        private System.Timers.Timer timer;
        private int timer_value;

        ELOAM_DEVCHANGECALLBACK DevChangeCallBack;
        ELOAM_ATTACHCALLBAK attachCallback;
        public Form1()
        {
            InitializeComponent();
            m_hView = IntPtr.Zero;
            m_hThumb = IntPtr.Zero;
            m_hView = IntPtr.Zero;
            m_hImageTemp = IntPtr.Zero;
            m_vDevice = new List<HELOAMDEVICE>();

            timer_value = 5;
            edit_Timer.Text = timer_value.ToString();

            pictureSavePath.Text = "D:";
            videoSavePath.Text = "D:";
            init();

            /*openVideo_Click(new object(), new EventArgs());*/
        }

        public void init()
        {
            m_hView = EloamDll.EloamGlobal_CreateView(eloamView.Handle, IntPtr.Zero, 0);
            m_hThumb = EloamDll.EloamGlobal_CreateThumbnail(eloamThumbnail.Handle, IntPtr.Zero, 0);

            DevChangeCallBack =
                 new ELOAM_DEVCHANGECALLBACK(DEVCHANGECALLBACK);
            EloamDll.EloamGlobal_InitDevs(DevChangeCallBack, this.Handle);

            if (1 != EloamDll.EloamGlobal_VideoCapInit())
            {
                MessageBox.Show("初始化视频录像失败！");
            }

            if (1 == EloamDll.EloamGlobal_InitBarcode())
            {
                barcode.Enabled = true;
            }
            else
            {
                barcode.Enabled = false;
            }

            EloamDll.EloamGlobal_InitFaceDetect();
            Reset();
        }

        public void DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData)
        {
            if (1 != type)	// 只处理视频设备
                return;

            if (1 == dbt) // 设备到达
            {
                HELOAMDEVICE hDev = EloamDll.EloamGlobal_CreateDevice(1, idx);
                m_vDevice.Add(hDev);

                // 显示到界面
                IntPtr temp = EloamDll.EloamDevice_GetFriendlyName(hDev);
                string wName = Marshal.PtrToStringAuto(temp);
                selectDevice.Items.Add(wName);

                //销毁字符串所占空间
                EloamDll.EloamGlobal_DestroyString(temp);

                if (-1 == selectDevice.SelectedIndex)
                {
                    selectDevice.SelectedIndex = 0;//改变所选设备
                }

                selectDevice_SelectedIndexChanged(new object(), new EventArgs());
            }
            else if (2 == dbt) // 设备丢失
            {
                int realIdx = -1;
                for (int i = 0; i < m_vDevice.Count(); i++)
                {
                    if (idx == EloamDll.EloamDevice_GetIndex(m_vDevice[i]))
                    {
                        realIdx = i;
                        break;
                    }
                }

                if (realIdx == -1)
                {
                    return;
                }

                HELOAMDEVICE hDevTemp = EloamDll.EloamVideo_GetDevice(m_hVideo);
                if (m_vDevice[realIdx] == hDevTemp)
                {
                    closeVideo_Click(new object(), new EventArgs());
                }

                EloamDll.EloamDevice_Release(hDevTemp);
                EloamDll.EloamDevice_Release(m_vDevice[realIdx]);

                m_vDevice.RemoveAt(realIdx);
                selectDevice.Items.RemoveAt(realIdx);

                if (-1 == selectDevice.SelectedIndex)
                {
                    selectDevice.Text = null;
                    if (0 != m_vDevice.Count)
                    {
                        selectDevice.SelectedIndex = 0;
                    }
                    else
                    {
                        selectMode.Items.Clear();
                        selectResolution.Items.Clear();
                    }
                }
            }
        }

        private void closeVideo_Click(object sender, EventArgs e)
        {
            EloamDll.EloamView_SetText(m_hView, null, 0);
            EloamDll.EloamVideo_Release(m_hVideo);
            m_hVideo = IntPtr.Zero;

            Reset();

            EloamDll.EloamImage_Release(m_hImageTemp);
            m_hImageTemp = IntPtr.Zero;

            //kill timer
            if (null != timer)
            {
                timer.Stop();
                timer.Enabled = false;
            }
            edit_Timer.Enabled = true;
        }

        public void Reset()
        {
            selectDevice.Enabled = true;
            selectResolution.Enabled = true;
            selectMode.Enabled = true;

            openVideo.Enabled = true;
            closeVideo.Enabled = false;
            turnLeft.Enabled = false;
            turnRight.Enabled = false;
            exchangeLeftRight.Enabled = false;
            exchangeUpDown.Enabled = false;
            openProperty.Enabled = false;

            rectify.Enabled = false;
            rectify.Checked = false;
            removeGround.Enabled = false;
            removeGround.Checked = false;
            autoShoot.Enabled = false;
            autoShoot.Checked = false;

            openTimer.Enabled = false;
            openTimer.Checked = false;
            edit_Timer.Enabled = false;

            compoundShoot.Enabled = false;
            compoundShoot.Checked = false;

            pictureSavePath.Enabled = false;
            videoSavePath.Enabled = false;
            shoot.Enabled = false;

            barcode.Enabled = false;

        }

        public uint RGB(uint r, uint g, uint b)
        {
            return (((b << 16) | (g << 8)) | r);
        }

        private void openVideo_Click(object sender, EventArgs e)
        {
            int devIdx = selectDevice.SelectedIndex;
            int resIdx = selectResolution.SelectedIndex;
            string curModeString = selectMode.SelectedItem.ToString();
            int modeIdx = (curModeString == "YUY2" ? 1 :
                            (curModeString == "MJPG" ? 2 :
                                (curModeString == "UYVY" ? 4 :
                                    -1)));

            if (-1 != devIdx)
            {
                EloamDll.EloamVideo_Release(m_hVideo);

                HELOAMDEVICE hDev = m_vDevice[devIdx];
                m_hVideo = EloamDll.EloamDevice_CreateVideo(
                    hDev, resIdx, modeIdx, null, (IntPtr)0, null, (IntPtr)0,0,2);

                if (null != m_hVideo)
                {
                    attachCallback = new ELOAM_ATTACHCALLBAK(ATTACHCALLBAK);
                    EloamDll.EloamView_SelectVideo(m_hView, m_hVideo, attachCallback, this.Handle);
                    EloamDll.EloamView_SetText(m_hView, "打开视频中，请等待...", RGB(255, 255, 255));

                    selectDevice.Enabled = false;
                    selectResolution.Enabled = false;
                    selectMode.Enabled = false;

                    openVideo.Enabled = false;
                    closeVideo.Enabled = true;
                }
            }
        }

        public void ATTACHCALLBAK(HELOAMVIDEO video, LONG videoId, HELOAMVIEW view, LONG viewId, LPVOID userData)
        {
            if (0 == videoId && 0 == viewId)
            {
                openVideo.Enabled = false;
                closeVideo.Enabled = true;
                turnLeft.Enabled = true;
                turnRight.Enabled = true;
                exchangeLeftRight.Enabled = true;
                exchangeUpDown.Enabled = true;
                openProperty.Enabled = true;

                rectify.Enabled = true;
                removeGround.Enabled = true;
                autoShoot.Enabled = true;

                openTimer.Enabled = true;
                edit_Timer.Enabled = true;

                compoundShoot.Enabled = true;

                pictureSavePath.Enabled = true;

                videoSavePath.Enabled = true;

                shoot.Enabled = true;

                barcode.Enabled = true;
            }
        }

        private void selectDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMode.Items.Clear();

            int idx = selectDevice.SelectedIndex;//记录当前所选设备
            if (-1 != idx)
            {
                HELOAMDEVICE hDev = m_vDevice[idx];

                //加载模式列表
                LONG subtype = EloamDll.EloamDevice_GetSubtype(hDev);
                if (0 != (subtype & 1))
                {
                    selectMode.Items.Add("YUY2");
                }
                if (0 != (subtype & 2))
                {
                    selectMode.Items.Add("MJPG");
                }
                if (0 != (subtype & 4))
                {
                    selectMode.Items.Add("UYVY");
                }

                //若为辅摄像头，优先选择MJPG方式
                if (1 != EloamDll.EloamDevice_GetEloamType(hDev) && 0 != (subtype & 2))
                {
                    selectMode.SelectedIndex = 1;
                }
                else
                {
                    selectMode.SelectedIndex = 0;
                }

                selectMode_SelectedIndexChanged(new object(), new EventArgs());
                openVideo.Enabled = true;

            }
            else
            {
                openVideo.Enabled = false;
            }
        }

        private void selectMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectResolution.Items.Clear();
            int idx = selectDevice.SelectedIndex;//记录当前所选设备
            if (-1 != idx)
            {
                HELOAMDEVICE hDev = m_vDevice[idx];

                //加载分辨率列表
                int subtype = -1;
                string curModeString = selectMode.SelectedItem.ToString();
                subtype = (curModeString == "YUY2" ? 1 :
                            (curModeString == "MJPG" ? 2 :
                                (curModeString == "UYVY" ? 4 :
                                    -1)));

                if (-1 != subtype)
                {
                    int count = EloamDll.EloamDevice_GetResolutionCountEx(hDev, subtype);
                    for (int i = 0; i < count; ++i)
                    {
                        LONG width = EloamDll.EloamDevice_GetResolutionWidthEx(hDev, subtype, i);
                        LONG height = EloamDll.EloamDevice_GetResolutionHeightEx(hDev, subtype, i);

                        string str = width + " * " + height;
                        selectResolution.Items.Add(str);
                    }
                    selectResolution.SelectedIndex = 0;
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            closeVideo_Click(new object(), new EventArgs());

            int size = m_vDevice.Count;
            for (int i = 0; i < size; ++i)
            {
                HELOAMDEVICE hDev = m_vDevice[i];
                EloamDll.EloamDevice_Release(hDev);
            }
            EloamDll.EloamGlobal_DeinitFaceDetect();
            EloamDll.EloamThumbnail_Release(m_hThumb);
            EloamDll.EloamView_Release(m_hView);
            EloamDll.EloamGlobal_DeinitBarcode();
            EloamDll.EloamGlobal_DeinitDevs();

       
        }

        private void turnLeft_Click(object sender, EventArgs e)
        {
            EloamDll.EloamVideo_RotateLeft(m_hVideo);
        }

        private void turnRight_Click(object sender, EventArgs e)
        {
            EloamDll.EloamVideo_RotateRight(m_hVideo);
        }

        private void exchangeLeftRight_Click(object sender, EventArgs e)
        {
            EloamDll.EloamVideo_Mirror(m_hVideo);
        }

        private void exchangeUpDown_Click(object sender, EventArgs e)
        {
            EloamDll.EloamVideo_Flip(m_hVideo);
        }

        private void openProperty_Click(object sender, EventArgs e)
        {
            HELOAMDEVICE hDev = EloamDll.EloamVideo_GetDevice(m_hVideo);
            EloamDll.EloamDevice_ShowProperty(hDev, this.Handle);
            EloamDll.EloamDevice_Release(hDev);
        }

        private void rectify_CheckedChanged(object sender, EventArgs e)
        {
            if (rectify.Checked)
            {
                EloamDll.EloamVideo_EnableDeskew(m_hVideo, 0,0);
            }
            else
            {
                EloamDll.EloamVideo_DisableDeskew(m_hVideo);
            }
        }

        private void removeGround_CheckedChanged(object sender, EventArgs e)
        {
            if (removeGround.Checked)
            {
                EloamDll.EloamVideo_EnableDelBkColor(m_hVideo, 0);
            }
            else
            {
                EloamDll.EloamVideo_DisableDelBkColor(m_hVideo);
            }
        }

        public void MOVEDETECCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData)
        {
            shoot_Click(new object(), new EventArgs());
        }

        private void autoShoot_CheckedChanged(object sender, EventArgs e)
        {
            if (autoShoot.Checked)
            {
                ELOAM_MOVEDETECCALLBACK MoveDetecCallback =
                    new ELOAM_MOVEDETECCALLBACK(MOVEDETECCALLBACK);
                EloamDll.EloamVideo_EnableMoveDetec(m_hVideo, 0, MoveDetecCallback, this.Handle);
            }
            else
            {
                EloamDll.EloamVideo_DisableMoveDetec(m_hVideo);
            }
        }

        private void shoot_Click(object sender, EventArgs e)
        {
            HELOAMIMAGE hImg = EloamDll.EloamVideo_CreateImage(
                m_hVideo, 0, m_hView);
           
            if (IntPtr.Zero != hImg)
            {
                //EloamDll.EloamImage_DelImageBackColor(hImg, (HELOAMRECT)0);
                //EloamDll.EloamImage_DelImageBackColor(hImg);
                DateTime dateTime = DateTime.Now;
                string time = DateTime.Now.ToString(
                    "yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
                string filename = pictureSavePath.Text + "//" + time + ".jpg";

                // 是否启用合成拍照
                if (compoundShoot.Checked)
                {
                    if (IntPtr.Zero == m_hImageTemp)
                    {
                        m_hImageTemp = hImg;
                        MessageBox.Show("<合成拍照中，请换第二张>");
                    }
                    else
                    {
                        // 合成
                        LONG w1 = EloamDll.EloamImage_GetWidth(m_hImageTemp);
                        LONG w2 = EloamDll.EloamImage_GetWidth(hImg);
                        LONG w = (w1 > w2) ? w1 : w2;

                        LONG h1 = EloamDll.EloamImage_GetHeight(m_hImageTemp);
                        LONG h2 = EloamDll.EloamImage_GetHeight(hImg);
                        LONG h = h1 + h2;

                        HELOAMIMAGE hImage = EloamDll.EloamGlobal_CreateImage(w, h, 3);
                        if (IntPtr.Zero != hImage)
                        {
                            HELOAMRECT rectDest1 = EloamDll.EloamGlobal_CreateRect(0, 0, w1, h1);
                            HELOAMRECT rectSrc1 = EloamDll.EloamGlobal_CreateRect(0, 0, w1, h1);
                            EloamDll.EloamImage_Blend(hImage, rectDest1, m_hImageTemp, rectSrc1, 0, 0);
                            EloamDll.EloamRect_Release(rectDest1);
                            EloamDll.EloamRect_Release(rectSrc1);

                            HELOAMRECT rectDest2 = EloamDll.EloamGlobal_CreateRect(0, h1, w2, h2);
                            HELOAMRECT rectSrc2 = EloamDll.EloamGlobal_CreateRect(0, 0, w2, h2);
                            EloamDll.EloamImage_Blend(hImage, rectDest2, hImg, rectSrc2, 0, 0);
                            EloamDll.EloamRect_Release(rectDest2);
                            EloamDll.EloamRect_Release(rectSrc2);

                            if (1 == EloamDll.EloamImage_Save(hImage, filename, 0))
                            {
                                EloamDll.EloamThumbnail_Add(m_hThumb, filename);
                            }
                            EloamDll.EloamImage_Release(hImage);
                        }

                        EloamDll.EloamImage_Release(m_hImageTemp);
                        m_hImageTemp = IntPtr.Zero;
                        EloamDll.EloamImage_Release(hImg);
                    }
                }
                else
                {
                    if (1 == EloamDll.EloamImage_Save(hImg, filename, 0))
                    {
                        EloamDll.EloamView_PlayCaptureEffect(m_hView);
                        EloamDll.EloamThumbnail_Add(m_hThumb, filename);

                        if (barcode.Checked)
                        {
                            EloamDll.EloamGlobal_DiscernBarcode(hImg);
                            LONG count = EloamDll.EloamGlobal_GetBarcodeCount();
                            if (count > 0)
                            {
                                string strMsg = "";
                                for (LONG i = 0; i < count; ++i)
                                {
                                    strMsg += EloamDll.EloamGlobal_GetBarcodeData(i) + "\r\n";
                                }

                                MessageBox.Show(strMsg);
                            }
                            else
                            {
                                MessageBox.Show("识别条码失败");
                            }
                        }
                    }

                    EloamDll.EloamImage_Release(hImg);
                }
            }

        }

        private void openTimer_CheckedChanged(object sender, EventArgs e)
        {
            if (openTimer.Checked)
            {
                edit_Timer.Enabled = false;

                timer = new System.Timers.Timer();
                timer.Enabled = true;
                timer.Interval =
                    uint.Parse(edit_Timer.Text) * 1000;//单位为毫秒
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

            }
            else
            {
                timer.Stop();
                timer.Enabled = false;
                edit_Timer.Enabled = true;
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            shoot_Click(sender, e);
        }

        private void edit_Timer_TextChanged(object sender, EventArgs e)
        {
            try
            {
                uint.Parse(edit_Timer.Text);
            }
            catch (System.Exception)
            {
                MessageBox.Show("请输入一个正整数");
                edit_Timer.Text = "5";
            }
        }

        private void compoundShoot_CheckedChanged(object sender, EventArgs e)
        {
            if (!compoundShoot.Checked)
            {
                if (IntPtr.Zero != m_hImageTemp)
                {
                    EloamDll.EloamImage_Release(m_hImageTemp);
                    m_hImageTemp = IntPtr.Zero;
                }
            }
        }

        private void Recorde_Click(object sender, EventArgs e)
        {
            if (IntPtr.Zero != m_hVideo)
            {
                int videoOutputWidth = EloamDll.EloamVideo_GetWidth(m_hVideo);
                int videoOutputHeight = EloamDll.EloamVideo_GetHeight(m_hVideo);
                //录像时，打开视频的分辨率越低，帧率越高,一般不超过200w像素
                //所设置的帧率应尽可能高于实际帧率，避免丢帧
                const int FrameRate = 15;//此参数可根据录像分辨率与机型实际帧率调节
                const int CheckWidth = 1600;
                const int CheckHeight = 1200;
                //主流视频分辨率少有大于200万的，因此为节约cpu资源，当分辨率大于200w，应采用200w的配置录制
                if ((videoOutputWidth * videoOutputHeight) > (CheckWidth * CheckHeight))
                {
                    MessageBoxButtons msgButton = MessageBoxButtons.YesNo;
                    DialogResult dr = MessageBox.Show(
                        "视频分辨率小于200万时，录制效果最佳，建议您切换到1600X1200或相近分辨率后继续录制视频",
                        "录像", msgButton);
                    if (dr == DialogResult.Yes)//如果点击“确定”按钮
                    {
                        return;
                    }
                    videoOutputWidth = CheckWidth;
                    videoOutputHeight = CheckHeight;
                }

                if (IntPtr.Zero != m_cap)
                {
                    EloamDll.EloamGlobal_VideoCapStop(m_cap);
                    EloamDll.EloamGlobal_DestroyVideoCap(m_cap);
                    m_cap = IntPtr.Zero;
                }

                m_cap = EloamDll.EloamGlobal_CreatVideoCap();
                if (IntPtr.Zero == m_cap)
                {
                    MessageBox.Show("创建录像对象失败");
                    return;
                }

                int nSelectMic = -1;
                if (EloamDll.EloamGlobal_VideoCapGetAudioDevNum() > 0)//有麦克风
                {
                    nSelectMic = 0;
                }
                DateTime dateTime = DateTime.Now;
                string time = DateTime.Now.ToString(
                    "yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
                string fileName = videoSavePath.Text + "//" + time + ".avi";
                if (1 != EloamDll.EloamGlobal_VideoCapPreCap(
                    m_cap,
                   fileName,
                   nSelectMic,
                   FrameRate,
                    1,
                    videoOutputWidth,
                    videoOutputHeight,
                    1))
                {
                    MessageBox.Show("录像预处理失败");
                    return;
                }
                if (1 != EloamDll.EloamGlobal_VideoCapAddVideoSrc(m_cap, m_hVideo))
                {
                    MessageBox.Show("添加视频源失败");
                    return;
                }
                if (1 != EloamDll.EloamGlobal_VideoCapStart(m_cap))
                {
                    MessageBox.Show("开始录像失败");
                    return;
                }
                else
                {
                    MessageBox.Show("开始录像");
                }
            }

        }

        private void StopRecorde_Click(object sender, EventArgs e)
        {
            if (IntPtr.Zero != m_hVideo)
            {
                if (IntPtr.Zero != m_cap)
                {
                    EloamDll.EloamGlobal_VideoCapStop(m_cap);
                    EloamDll.EloamGlobal_DestroyVideoCap(m_cap);
                    m_cap = IntPtr.Zero;
                    MessageBox.Show("录像已停止");
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (1 == EloamDll.EloamGlobal_InitIdCard(null, IntPtr.Zero))
            {
                if (1 == EloamDll.EloamGlobal_ReadIdCard())
                {
                    string strMsg = "";
                    for (LONG i = 0; i < 17; ++i)
                    {
                        strMsg += EloamDll.EloamGlobal_GetIdCardData(i) + "\r\n";
                    }
                    HELOAMIMAGE hImg =EloamDll.EloamGlobal_GetIdCardImage(1);
                    EloamDll.EloamImage_Save(hImg, "C:\\IDCard.jpg", 0);
                    EloamDll.EloamImage_Release(hImg);
                    MessageBox.Show(strMsg);
                }
                else
                {
                    MessageBox.Show("启动二代证阅读失败！");
                }
                EloamDll.EloamGlobal_DeinitIdCard();
            }
            else
            {
                MessageBox.Show("初始化二代证失败，请检查是否已开启二代证阅读模式，或二代证驱动是否安装！");
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (rectify.Checked)
            {
                EloamDll.EloamGlobal_EnableFaceRectCrop(m_hVideo, 1);
            }
            else
            {
                EloamDll.EloamGlobal_DisableFaceRectCrop(m_hVideo);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HELOAMIMAGE hImg = IntPtr.Zero;
            //获取身份证头像
            if (1 == EloamDll.EloamGlobal_InitIdCard(null, (IntPtr)0))
            {
                if (1 == EloamDll.EloamGlobal_ReadIdCard())
                {
                    string strMsg = "";
                    for (LONG i = 0; i < 17; ++i)
                    {
                        strMsg += EloamDll.EloamGlobal_GetIdCardData(i) + "\r\n";
                    }
                    hImg = EloamDll.EloamGlobal_GetIdCardImage(1);
                    //EloamDll.EloamImage_Save(hImg, "C:\\IDCard.jpg", 0);
                   
                    MessageBox.Show(strMsg);
                }
                else
                {
                    MessageBox.Show("启动二代证阅读失败！");
                }
                EloamDll.EloamGlobal_DeinitIdCard();
            }
            else
            {
                MessageBox.Show("初始化二代证失败，请检查是否已开启二代证阅读模式，或二代证驱动是否安装！");
            }
            //获取摄像头 人脸
            if (IntPtr.Zero != m_hVideo)
            {
                EloamDll.EloamGlobal_EnableFaceRectCrop(m_hVideo, 1);
                HELOAMIMAGE hImg2 = EloamDll.EloamVideo_CreateImage(m_hVideo, 0, m_hView);
                if (IntPtr.Zero != hImg)
                {
                    LONG ret = EloamDll.EloamGlobal_DiscernFaceDetect(hImg, hImg2);
                    EloamDll.EloamImage_Release(hImg2);
                    string show = "";
                    show += "相似度为:";
                    show += ret.ToString();
                    show += "（范围在0至100，一般认为大于50，则匹配成功）";
                    MessageBox.Show(show);
                }
                else
                {
                    MessageBox.Show("请打开副摄像头");
                }
            }

            if (IntPtr.Zero != hImg)
            {
                EloamDll.EloamImage_Release(hImg);
            }
        }
    }
}



